package by.blog.service;

import by.blog.dao.beans.Category;
import by.blog.dao.beans.Film;
import by.blog.dao.dao.DaoCategory;

import java.util.List;

/**
 * Бондарчик Антон  15.11.2014.
 */
public class CategoryService implements ServiceInterface<Category, Integer> {
    private DaoCategory daoCategory;
    private FilmService filmService;

    private List<Category> listOfAllCategories;

    public CategoryService() {
        daoCategory = new DaoCategory();
    }

    @Override
    public synchronized boolean createUnit(Category category) {
        boolean state = daoCategory.createUnit(category);
        listOfAllCategories = daoCategory.readAllUnits();
        return state;
    }

    @Override
    public Category readUnit(Integer INDEX) {
        return daoCategory.readUnit(INDEX);
    }

    public Category readUnit(String CATEGORY_NAME) {
        return daoCategory.readUnit(CATEGORY_NAME);
    }

    @Override
    public synchronized boolean updateUnit(Category category) {
        boolean state = daoCategory.updateUnit(category);
        listOfAllCategories = daoCategory.readAllUnits();
        return state;
    }

    @Override
    public synchronized boolean deleteUnit(Integer INDEX) {
        filmService = new FilmService();
        List<Film> filmsByDeletingParent = filmService.readUnitsByParent(INDEX);

        for (Film film : filmsByDeletingParent) {
            film.setCategory_id(0);                     // подумать куда записывать
        }

        boolean state = daoCategory.deleteUnit(INDEX);
        listOfAllCategories = daoCategory.readAllUnits();
        return state;
    }

    public List<Category> readAllUnits() {
        List<Category> allCategories = daoCategory.readAllUnits();
        return allCategories;
    }

    public List<Category> getListOfAllCategories() {
        return listOfAllCategories;
    }

    public synchronized void setListOfAllCategories(List<Category> listOfAllCategories) {
        this.listOfAllCategories = listOfAllCategories;
    }

}
